//
// Created by work on 24-6-5.
//

#include "iot_model_device/CModelInfo.h"

namespace iotplat {

    std::map<CModelInfo::EModelCol, std::string> CModelInfo::rtModelColToStringMap = {
            {CModelInfo::EModelCol::EName, "name"},
            {CModelInfo::EModelCol::EUser, "userid"},
            {CModelInfo::EModelCol::EDescr, "descr"},
            {CModelInfo::EModelCol::ESn, "sn"},
            {CModelInfo::EModelCol::EGroupIngs, "groupings"},
            {CModelInfo::EModelCol::ELocation, "location"},
            {CModelInfo::EModelCol::ECreate_time, "create_time"},
            {CModelInfo::EModelCol::EUpdate_time, "update_time"},
            {CModelInfo::EModelCol::EProducer, "producer"},
            {CModelInfo::EModelCol::EValid, "vaild"},
            {CModelInfo::EModelCol::EConfigs, "configs"}
    };

    std::map<CModelInfo::EModelCol, CValue::EValueType> CModelInfo::rtModelDataTypeByField = {
            {CModelInfo::EModelCol::EName, CValue::IOT_String},
            {CModelInfo::EModelCol::EUser, CValue::IOT_String},
            {CModelInfo::EModelCol::EDescr, CValue::IOT_String},
            {CModelInfo::EModelCol::ESn, CValue::IOT_Int},
            {CModelInfo::EModelCol::EGroupIngs, CValue::IOT_String},
            {CModelInfo::EModelCol::ELocation, CValue::IOT_String},
            {CModelInfo::EModelCol::ECreate_time, CValue::IOT_DateTime},
            {CModelInfo::EModelCol::EUpdate_time, CValue::IOT_DateTime},
            {CModelInfo::EModelCol::EProducer, CValue::IOT_String},
            {CModelInfo::EModelCol::EValid, CValue::IOT_Bool},
            {CModelInfo::EModelCol::EConfigs, CValue::IOT_Json}
    };

    CModelInfo::CModelInfo() {
    }

    CValue CModelInfo::getValueByField(CModelInfo::EModelCol field) {
        switch (field) {
            case EModelCol::EName:
                return CValue(name);
                break;
            case EModelCol::EUser:
                return CValue(user);
                break;
            case EModelCol::EDescr:
                return CValue(descr);
                break;
            case EModelCol::ESn:
                return CValue(sn);
                break;
            case EModelCol::EGroupIngs:
                return CValue(grouping);
                break;
            case EModelCol::ELocation:
                return CValue(location);
                break;
            case EModelCol::ECreate_time:
                return CValue(create_time);
                break;
            case EModelCol::EUpdate_time:
                return CValue(update_time);
                break;
            case EModelCol::EProducer:
                return CValue(producer);
                break;
            case EModelCol::EValid:
                return CValue(valid);
                break;
            case EModelCol::EConfigs:
                return CValue(configs);
                break;
            default:
                break;
        }
        return CValue();
    }

    Json::Value CModelInfo::toJson() {
        Json::Value resmodel(Json::objectValue);
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::EUser)->second] = user;
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::EName)->second] = name;
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::EDescr)->second] = descr;
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::ESn)->second] = sn;
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::ECreate_time)->second] = create_time.ToString();
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::EUpdate_time)->second] = update_time.ToString();
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::EGroupIngs)->second] = grouping;
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::ELocation)->second] = location;
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::EProducer)->second] = producer;
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::EValid)->second] = valid;
        resmodel[rtModelColToStringMap.find(CModelInfo::EModelCol::EConfigs)->second] = configs;
        return resmodel;
    }

    void CModelInfo::fromJson(Json::Value json) {
        name = json[rtModelColToStringMap.find(CModelInfo::EModelCol::EName)->second].asString();
        user = json[rtModelColToStringMap.find(CModelInfo::EModelCol::EUser)->second].asString();
        descr = json[rtModelColToStringMap.find(CModelInfo::EModelCol::EDescr)->second].asString();
        sn = json[rtModelColToStringMap.find(CModelInfo::EModelCol::ESn)->second].asInt();
        grouping = json[rtModelColToStringMap.find(CModelInfo::EModelCol::EGroupIngs)->second].asString();
        location = json[rtModelColToStringMap.find(CModelInfo::EModelCol::ELocation)->second].asString();
        create_time = CDateTime::FromString(json[rtModelColToStringMap.find(CModelInfo::EModelCol::ECreate_time)->second].asString());
        update_time = CDateTime::FromString(json[rtModelColToStringMap.find(CModelInfo::EModelCol::EUpdate_time)->second].asString());
        producer = json[rtModelColToStringMap.find(CModelInfo::EModelCol::EProducer)->second].asString();
        valid = json[rtModelColToStringMap.find(CModelInfo::EModelCol::EValid)->second].asBool();
        configs = json[rtModelColToStringMap.find(CModelInfo::EModelCol::EConfigs)->second];
    }
} // iotplat